package com.hngy.xpq.dianming;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.Random;

public class MainActivity3 extends AppCompatActivity implements View.OnClickListener {
    TextView text123, text04;
    Button button123, button234, button345, button456;
    int yyx1 = 0, yyx2 = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        text123 = findViewById(R.id.main_text123);
        button123 = findViewById(R.id.button_07);
        button234 = findViewById(R.id.button_08);
        button345 = findViewById(R.id.button_09);
        button456 = findViewById(R.id.button_10);
        text04 = findViewById(R.id.text_04);

        text123.setOnClickListener(this);
        button123.setOnClickListener(this);
        button234.setOnClickListener(this);
        button345.setOnClickListener(this);
        button456.setOnClickListener(this);
        text04.setOnClickListener(this);
        text04.setMovementMethod(ScrollingMovementMethod.getInstance());

    }

    @Override
    public void onClick(View v) {
        String[] indexXingYun1 = getResources().getStringArray(R.array.yy19_1);
        String[] indexXingYun2 = getResources().getStringArray(R.array.yy19_2);
        String[] indexXingYun3 = getResources().getStringArray(R.array.XingYunbj);

        String text12 = text123.getText().toString();
        int indextext123 = Integer.valueOf(text12);
        int[] index;
        int[] index1;
        int kelang = 0;
        int banji = 0;
        String b = "";
        index = new int[indextext123];
        index1 = new int[indextext123];
        switch (v.getId()) {
            case R.id.button_07:
                Intent intent = new Intent(MainActivity3.this, MainActivity_lord.class);
                startActivity(intent);
                break;
            case R.id.button_08:
                text04.setText(null);
                for (int x = 0; x < indextext123; x++) {
                    Random XingYun = new Random();
                    kelang = XingYun.nextInt(42) + 1;
                    for (int y = 0; y < x; y++) {
                        for (; 0 < 1; ) {
                            if (kelang == 3 || kelang == 9 || kelang == 26 || kelang == 33) {
                                kelang = XingYun.nextInt(42) + 1;
                            } else {
                                break;
                            }
                        }
                        if (index[y] == kelang) {
                            kelang = XingYun.nextInt(42) + 1;
                            y = 0;
                        }
                    }
                    index[x] = kelang;
                }
                for (int x = 0; x < indextext123; x++) {
                    String a = "";
                    for (int y = indextext123 - 1; y > x; y--) {
                        if (index[x] > index[y]) {
                            int t = index[x];
                            index[x] = index[y];
                            index[y] = t;
                        }
                    }
                    a = String.valueOf(index[x]);
                    if (a.length() == 1) {
                        a = "0" + a;
                    }
                    b = text04.getText().toString();
                    text04.setText(b + "          学号： " + a + "  姓名：  " + indexXingYun1[index[x]] + '\n');
                }
                break;
            case R.id.button_09:
                text04.setText(null);
                if (indextext123 > 39) {
                    Toast.makeText(MainActivity3.this, "超过班级人数！！", Toast.LENGTH_SHORT).show();
                } else {
                    for (int x = 0; x < indextext123; x++) {
                        Random XingYun = new Random();
                        kelang = XingYun.nextInt(39) + 1;
                        for (int y = 0; y <= x; y++) {
                            if (index[y] == kelang) {
                                kelang = XingYun.nextInt(39) + 1;
                                y = 0;
                            }
                        }
                        index[x] = kelang;
                    }
                    for (int x = 0; x < indextext123; x++) {
                        String a = "";
                        for (int y = indextext123 - 1; y > x; y--) {
                            if (index[x] > index[y]) {
                                int t = index[x];
                                index[x] = index[y];
                                index[y] = t;
                            }
                        }
                        a = String.valueOf(index[x]);
                        if (a.length() == 1) {
                            a = "0" + a;
                        }
                        b = text04.getText().toString();
                        text04.setText(b + "          学号： " + a + "  姓名：  " + indexXingYun2[index[x]] + '\n');
                    }
                }
                break;
            case R.id.button_10:
                if (indextext123 > 39) {
                    Toast.makeText(MainActivity3.this, "人数过多", Toast.LENGTH_SHORT).show();
                } else {
                    text04.setText(null);
                    Random XingYun = new Random();
                    banji = XingYun.nextInt(indextext123);
                    for (int x = 0; x < banji; x++) {
                        kelang = XingYun.nextInt(42) + 1;
                        for (int y = 0; y <= banji; y++) {
                            if (index[y] == kelang) {
                                kelang = XingYun.nextInt(42) + 1;
                            }
                        }
                        index[x] = kelang;
                    }
                    for (int x = 0; x < indextext123 - banji; x++) {
                        kelang = XingYun.nextInt(39) + 1;
                        for (int y = 0; y <= x; y++) {
                            if (index1[y] == kelang) {
                                kelang = XingYun.nextInt(39) + 1;
                                y = 0;
                            }
                        }
                        index1[x] = kelang;
                    }
                    for (int x = 0; x < banji; x++) {
                        String a = "";
                        for (int y = banji - 1; y > x; y--) {
                            if (index[x] > index[y]) {
                                int t = index[x];
                                index[x] = index[y];
                                index[y] = t;
                            }
                        }
                        a = String.valueOf(index[x]);
                        if (a.length() == 1) {
                            a = "0" + a;
                        }
                        b = text04.getText().toString();
                        text04.setText(b + "    " + indexXingYun3[1] + "  学号：" + a + "  名字:  " + indexXingYun1[index[x]] + '\n');
                    }
                    for (int x = 0; x < indextext123 - banji; x++) {
                        String a = "";
                        for (int y = indextext123 - banji - 1; y > x; y--) {
                            if (index1[x] > index1[y]) {
                                int t = index1[x];
                                index1[x] = index1[y];
                                index1[y] = t;
                            }
                        }
                        a = String.valueOf(index1[x]);
                        if (a.length() == 1) {
                            a = "0" + a;
                        }
                        b = text04.getText().toString();
                        text04.setText(b + "    " + indexXingYun3[2] + "  学号：" + a + "  名字:  " + indexXingYun2[index1[x]] + '\n');
                    }
                }
                break;
        }
    }
}